package edu.uoc.tdp.pac4.client.pantalles;

import javax.swing.JPanel;

import edu.uoc.tdp.pac4.common.beans.S1Usuari;
import edu.uoc.tdp.pac4.common.beans.S4InfClients;
import edu.uoc.tdp.pac4.common.exception.ExcepcioConnexioBD;
import edu.uoc.tdp.pac4.common.exception.S4EstadistiquesException;
import edu.uoc.tdp.pac4.common.interfaces.S4InterfaceEstadistiques;
import edu.uoc.tdp.pac4.common.utils.TDSLanguageUtils;

import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JTable.PrintMode;
import javax.swing.JScrollPane;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.table.DefaultTableModel;
import javax.swing.JTextPane;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.print.PrinterException;
import java.rmi.RemoteException;
import java.text.MessageFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;


public class S4InformeClients extends JPanel {

	private static final long serialVersionUID = 1L;
	private JTable table;
	private S1Usuari _usuariConnectat;
	private S4InterfaceEstadistiques _iEstadistiques;
	private JTextPane txtNif;
	private JTextPane txtNom;
	private JTextPane txtCotxe;
	private JTextPane txtNumClient;

	/**
	 * Create the panel.
	 */
	public S4InformeClients(S4InterfaceEstadistiques iEstadistiques, S1Usuari usuariConnectat) {
		
		_usuariConnectat=usuariConnectat;
		_iEstadistiques=iEstadistiques;
		
		try {
			IniciaComponents();
		} catch (ParseException e) {
			JOptionPane.showMessageDialog(this, TDSLanguageUtils.getMessage("ERR_EST_INI"), "Error", JOptionPane.ERROR_MESSAGE);
		}

	}
	
	private void CercarClients(){

		List<S4InfClients> llistaClients = new ArrayList<S4InfClients>();
		
		try {
			Integer idClient;
			if(txtNumClient.getText().isEmpty()){idClient=null;}else{idClient=Integer.parseInt(txtNumClient.getText());}

			llistaClients=_iEstadistiques.CercarClients(_usuariConnectat.getTaller(), idClient, txtNif.getText(), txtNom.getText(), txtCotxe.getText());
			
			DefaultTableModel modelo=(DefaultTableModel) table.getModel();
			table.removeAll();
			Integer i;
			Integer num=modelo.getRowCount();
			for(i=num-1;i>=0;i--){
				modelo.removeRow(i);
			}
			
			if (llistaClients.size()>0 ){
				Integer filas=0;

				for (S4InfClients item : llistaClients){
										
					modelo.addRow(new Object[filas]);
					table.setValueAt(item.GetnumClient(),filas , 0);
					table.setValueAt(item.Getnif(), filas, 1);
					table.setValueAt(item.Getnom(), filas, 2);
					table.setValueAt(item.Getcognoms(), filas, 3);
					table.setValueAt(item.Gettelefon(), filas, 4);
					table.setValueAt(item.Getcotxe(), filas, 5);
					table.setValueAt(item.GetnumReparacions(), filas, 6);
					filas++;
	            }
				table.updateUI();
			}else{
				modelo.addRow(new Object[]{null, null, null, null, null, null, null, null,null,null,null});
				JOptionPane.showMessageDialog(this, TDSLanguageUtils.getMessage("ERR_EST_NODADES"), "Info", JOptionPane.INFORMATION_MESSAGE);
			}
		} catch (RemoteException e) {
			JOptionPane.showMessageDialog(this, TDSLanguageUtils.getMessage("ERR_RMI"), "Error", JOptionPane.ERROR_MESSAGE);
		} catch (S4EstadistiquesException e) {
			JOptionPane.showMessageDialog(this, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
		} catch (ExcepcioConnexioBD e) {
			JOptionPane.showMessageDialog(this, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
		}
	}
	
	private void PrintReport(){
		MessageFormat header=new MessageFormat(TDSLanguageUtils.getMessage("LBL_EST_INFCLI"));
		MessageFormat footer=new MessageFormat("- {0} -");
		try {
			table.print(PrintMode.FIT_WIDTH,header,footer);
		} catch (PrinterException e1) {
			JOptionPane.showMessageDialog(this, TDSLanguageUtils.getMessage("ERR_PRINT"), "Error", JOptionPane.ERROR_MESSAGE);
		}
	}
	
	private void IniciaComponents() throws ParseException {
		
		JLabel lblInformeDe = new JLabel(TDSLanguageUtils.getMessage("LBL_EST_INFCLI"));
		lblInformeDe.setFont(new Font("Tahoma", Font.BOLD, 17));
		
		JLabel lblDataDeFi = new JLabel(TDSLanguageUtils.getMessage("LBL_EST_NOM"));
		
		JButton button = new JButton(TDSLanguageUtils.getMessage("LBL_EST_CERCAR"));
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				CercarClients();
			}
		});
		
		JScrollPane scrollPane = new JScrollPane();
		
		JButton button_1 = new JButton(TDSLanguageUtils.getMessage("LBL_EST_IMPRIMIR"));
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				PrintReport();
			}
		});
								
												
		JLabel lblCotxe = new JLabel(TDSLanguageUtils.getMessage("LBL_EST_COTXE"));
		
		txtCotxe = new JTextPane();
		
		txtNom = new JTextPane();
		
		JLabel lblNif = new JLabel(TDSLanguageUtils.getMessage("LBL_EST_NCLIENT"));
		
		txtNif = new JTextPane();
		
		JLabel lblNif_1 = new JLabel("NIF");
		
		txtNumClient = new JTextPane();
		txtNumClient.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				char key=e.getKeyChar();
				if (!Character.isDigit(key) && key!= KeyEvent.VK_BACK_SPACE && key!= KeyEvent.VK_DELETE ){
					e.consume();
				}
			}
		});
		
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(button, Alignment.TRAILING, GroupLayout.PREFERRED_SIZE, 73, GroupLayout.PREFERRED_SIZE)
								.addComponent(lblInformeDe, GroupLayout.PREFERRED_SIZE, 301, GroupLayout.PREFERRED_SIZE)
								.addComponent(scrollPane, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 826, Short.MAX_VALUE))
							.addGap(26))
						.addGroup(groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(lblDataDeFi, GroupLayout.PREFERRED_SIZE, 63, GroupLayout.PREFERRED_SIZE)
								.addComponent(lblNif))
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(txtNom, GroupLayout.PREFERRED_SIZE, 184, GroupLayout.PREFERRED_SIZE)
								.addComponent(txtNumClient, GroupLayout.PREFERRED_SIZE, 108, GroupLayout.PREFERRED_SIZE))
							.addGap(27)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(lblNif_1, GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE)
								.addComponent(lblCotxe, GroupLayout.PREFERRED_SIZE, 38, GroupLayout.PREFERRED_SIZE))
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(txtCotxe, GroupLayout.PREFERRED_SIZE, 184, GroupLayout.PREFERRED_SIZE)
								.addComponent(txtNif, GroupLayout.PREFERRED_SIZE, 108, GroupLayout.PREFERRED_SIZE))
							.addGap(348))
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(button_1, GroupLayout.PREFERRED_SIZE, 98, GroupLayout.PREFERRED_SIZE)
							.addContainerGap(754, Short.MAX_VALUE))))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(11)
					.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(lblInformeDe, GroupLayout.PREFERRED_SIZE, 21, GroupLayout.PREFERRED_SIZE)
							.addGap(24)
							.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
								.addComponent(txtNif, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(lblNif)
								.addComponent(txtNumClient, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(lblNif_1))
							.addGap(11)
							.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
								.addComponent(txtNom, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(lblDataDeFi)
								.addComponent(txtCotxe, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(lblCotxe)))
						.addComponent(button))
					.addGap(18)
					.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 360, Short.MAX_VALUE)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(button_1)
					.addGap(6))
		);
		
		
		table = new JTable();
		table.setFont(new Font("Tahoma", Font.PLAIN, 10));
		table.setModel(new DefaultTableModel(
			new Object[][] {
				{null, null, null, null, null, null, null},
			},
			new String[] {
					TDSLanguageUtils.getMessage("LBL_EST_NCLIENT"), "NIF", TDSLanguageUtils.getMessage("LBL_EST_NOM"), TDSLanguageUtils.getMessage("LBL_EST_COGNOMS"), TDSLanguageUtils.getMessage("LBL_EST_TEL"), TDSLanguageUtils.getMessage("LBL_EST_COTXE"), TDSLanguageUtils.getMessage("LBL_EST_TREP")
			}
		));
		table.getColumnModel().getColumn(0).setPreferredWidth(50);
		table.getColumnModel().getColumn(1).setPreferredWidth(60);
		table.getColumnModel().getColumn(2).setPreferredWidth(100);
		table.getColumnModel().getColumn(3).setPreferredWidth(150);
		table.getColumnModel().getColumn(4).setPreferredWidth(70);
		table.getColumnModel().getColumn(5).setPreferredWidth(180);
		table.getColumnModel().getColumn(6).setPreferredWidth(100);
		scrollPane.setViewportView(table);
		setLayout(groupLayout);
	}
}
